package com.huitone.gddw.controller.monitoring.sms;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.common.excel.CreateExcel;
import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.History;
import com.huitone.gddw.dao.entity.SendSm;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.log.Log;
import com.huitone.gddw.log.LogResult;
import com.huitone.gddw.log.LogType;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.business.ISendSmService;
import com.huitone.gddw.service.dto.business.SendSmDTO;
import com.huitone.gddw.service.dto.monitoring.sms.AccessReportACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.AccessReportDTO;
import com.huitone.gddw.service.dto.monitoring.sms.AccessUpRepACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.AccessUpRepDTO;
import com.huitone.gddw.service.dto.monitoring.sms.SendSmLifeDTO;
import com.huitone.gddw.service.monitoring.sms.IAccessReportService;
import com.huitone.gddw.service.monitoring.sms.ISendSmLifeService;
import com.huitone.gddw.utils.SysLoginUtil;
//import com.huitone.smspfm.redis.impl.HashOpts;


@Controller
@RequestMapping(value="sendSmLife")
public class SendSmLifeController extends CommonController{
	
	
private final static Logger LOGGER = Logger.getLogger(AccessReportController.class);
	
	@Autowired
	private ISendSmLifeService sendSmLifeService;
	
	@Autowired
	private ISendSmService sendSmService;	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	@Log(logType=LogType.SELECT, content="查询短信发送生命周期明细")
	public QueryPage<SendSmLifeDTO> query(QueryPage<SendSmLifeDTO> page, HttpServletRequest request) {
		QueryPage<SendSmLifeDTO> result = null;
		try {
			page.setParams(getParams(request));
			result = sendSmLifeService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询短信发送生命周期明细出现异常：" + e.getMessage(), e);
			request.setAttribute("logResult", LogResult.EXCEPTION);
		}
		return result;
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String svrNode = request.getParameter("svrNode");
		String serviceCode = request.getParameter("serviceCode");
		String userNo = request.getParameter("userNo");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		
		String total = request.getParameter("total");
		String sendSuc = request.getParameter("sendSuc");
		String sendFail = request.getParameter("sendFail");
		String recvSuc = request.getParameter("recvSuc");
		String recvFail = request.getParameter("recvFail");
		String recvNone = request.getParameter("recvNone");
		String upTotal = request.getParameter("upTotal");
		
		String smSeq = request.getParameter("smSeq");
		String limitCount = request.getParameter("limitCount");
		
		String sendCode = request.getParameter("sendCode");
		String resultCode = request.getParameter("resultCode");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("svrNode", svrNode);
		params.put("serviceCode", serviceCode);
		params.put("userNo", userNo);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		
		params.put("total", total);
		params.put("sendSuc", sendSuc);
		params.put("sendFail", sendFail);
		params.put("recvSuc", recvSuc);
		params.put("recvFail", recvFail);
		params.put("recvNone", recvNone);
		params.put("upTotal", upTotal);
		
		params.put("smSeq", smSeq);
		params.put("limitCount", limitCount);
		
		params.put("sendCode", sendCode);
		params.put("resultCode", resultCode);
		return params;
	}
	
	
//	@ResponseBody
//	@RequestMapping(value="test")
//	public Map<String, Object> test(QueryPage<SendSmLifeDTO> qp) {
//		
//		Map<String, Object> params = qp.getParams();
//		
//		return params;
//		
//	}

	
	/**
	 * 详情
	 * @param recId 记录ID
	 * @return SendSmDTO
	 */
	@ResponseBody
	@RequestMapping(value="detail")
	public SendSmDTO detail(String recId) {
		try {
			return sendSmService.getDetial(recId);
		} catch (Exception e) {
			LOGGER.error("查询短信发送明细详情出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 导出 Excel
	 * @param request
	 * @param response
	 */
	//生命周期中没有用到
	@RequestMapping(value="/out-excel", method=RequestMethod.POST)
	public void outExcel(HttpServletRequest request, HttpServletResponse response) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "短信发送明细表";
		
		Map<String, Object> params = new HashMap<String, Object>();
		for (Object name : request.getParameterMap().keySet()) {
			params.put(name.toString(), request.getParameter(name.toString()));
		}
		
		List<SendSmDTO> list = sendSmService.getRecord(params);
		if (null == list || 0 == list.size()) {
			list.add(new SendSmDTO());
		}
		
		HSSFWorkbook wb = (HSSFWorkbook)CreateExcel
				.getInstance()
				.CreateNewExcelNoTemplate(fileName, list).getWorkbook();
		OutputStream fOut = null;
		try {
			fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename="+fileName+sdf.format(new Date())+".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}
	
	
	
	
	
	

}
